package com.atguigu.eduservice.controller.front;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.UcenterClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.frontvo.UcenterMemberInfo;
import com.atguigu.eduservice.service.EduCommentService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private EduCourseService courseService;

    //评论区的分页查询
    @GetMapping("pageComment/{id}/{limit}/{page}")
    public R addComment(@PathVariable String id,
                        @PathVariable long limit,
                        @PathVariable long page){
        Page<EduComment> pageParam=new Page<>(page,limit);
        QueryWrapper<EduComment> wrapper=new QueryWrapper<>();
        if(StringUtils.isEmpty(id)){
            throw new GuliException(20001,"显示错误");
        }
        wrapper.eq("course_id",id);
        wrapper.orderByDesc("gmt_create");
        commentService.page(pageParam,wrapper);
        List<EduComment> commentList = pageParam.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());
        return R.ok().data(map);
    }

    //添加评论
    @PostMapping("auth/save/{courseId}")
    public R save(@RequestBody EduComment eduComment, HttpServletRequest request,@PathVariable String courseId){
        String memberId= JwtUtils.getMemberIdByJwtToken(request);//根据请求头拿到id
        if(StringUtils.isEmpty(memberId)){
            throw new GuliException(20001,"请登录您的用户");
        }
        UcenterMemberInfo ucenterMemberInfo=ucenterClient.getInfo(memberId);//利用nacos调用ucenter中的方法
        BeanUtils.copyProperties(ucenterMemberInfo,eduComment);
        eduComment.setMemberId(memberId);
        EduCourse course=courseService.getById(courseId);
        eduComment.setCourseId(courseId);
        eduComment.setTeacherId(course.getTeacherId());
        commentService.save(eduComment);
        return R.ok();
    }
}

